home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tests / parse.test < prev    next >
Text File  |  1992-11-06  |  11KB  |  401 lines

  1. # Commands covered:  set (plus basic command syntax)
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /user6/ouster/tcl/tests/RCS/parse.test,v 1.21 91/10/31 16:40:37 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. proc fourArgs {a b c d} {
  21.     global arg1 arg2 arg3 arg4
  22.     set arg1 $a
  23.     set arg2 $b
  24.     set arg3 $c
  25.     set arg4 $d
  26. }
  27.  
  28. proc getArgs args {
  29.     global argv
  30.     set argv $args
  31. }
  32.  
  33. # Basic argument parsing.
  34.  
  35. test parse-1.1 {basic argument parsing} {
  36.     set arg1 {}
  37.     fourArgs a b    c          d
  38.     list $arg1 $arg2 $arg3 $arg4
  39. } {a b c d}
  40. test parse-1.2 {basic argument parsing} {
  41.     set arg1 {}
  42.     eval "fourArgs 123\v4\f56\r7890"
  43.     list $arg1 $arg2 $arg3 $arg4
  44. } {123 4 56 7890}
  45.  
  46. # Quotes.
  47.  
  48. test parse-2.1 {quotes and variable-substitution} {
  49.     getArgs "a b c" d
  50.     set argv
  51. } {{a b c} d}
  52. test parse-2.2 {quotes and variable-substitution} {
  53.     set a 101
  54.     getArgs "a$a b c"
  55.     set argv
  56. } {{a101 b c}}
  57. test parse-2.3 {quotes and variable-substitution} {
  58.     set argv "xy[format xabc]"
  59.     set argv
  60. } {xyxabc}
  61. test parse-2.4 {quotes and variable-substitution} {
  62.     set argv "xy\t"
  63.     set argv
  64. } xy\t
  65. test parse-2.5 {quotes and variable-substitution} {
  66.     set argv "a b    c
  67. d e f"
  68.     set argv
  69. } a\ b\tc\nd\ e\ f
  70. test parse-2.6 {quotes and variable-substitution} {
  71.     set argv a"bcd"e
  72.     set argv
  73. } {a"bcd"e}
  74.  
  75. # Braces.
  76.  
  77. test parse-3.1 {braces} {
  78.     getArgs {a b c} d
  79.     set argv
  80. } "{a b c} d"
  81. test parse-3.2 {braces} {
  82.     set a 101
  83.     set argv {a$a b c}
  84.     set b [string index $argv 1]
  85.     set b
  86. } {$}
  87. test parse-3.3 {braces} {
  88.     set argv {a[format xyz] b}
  89.     string length $argv
  90. } 15
  91. test parse-3.4 {braces} {
  92.     set argv {a\nb\}}
  93.     string length $argv
  94. } 6
  95. test parse-3.5 {braces} {
  96.     set argv {{{{}}}}
  97.     set argv
  98. } "{{{}}}"
  99. test parse-3.6 {braces} {
  100.     set argv a{{}}b
  101.     set argv
  102. } "a{{}}b"
  103. test parse-3.7 {braces} {
  104.     set a [format "last]"]
  105.     set a
  106. } {last]}
  107.  
  108. # Command substitution.
  109.  
  110. test parse-4.1 {command substitution} {
  111.     set a [format xyz]
  112.     set a
  113. } xyz
  114. test parse-4.2 {command substitution} {
  115.     set a a[format xyz]b[format q]
  116.     set a
  117. } axyzbq
  118. test parse-4.3 {command substitution} {
  119.     set a a[
  120. set b 22;
  121. format %s $b
  122.  
  123. ]b
  124.     set a
  125. } a22b
  126.  
  127. # Variable substitution.
  128.  
  129. test parse-5.1 {variable substitution} {
  130.     set a 123
  131.     set b $a
  132.     set b
  133. } 123
  134. test parse-5.2 {variable substitution} {
  135.     set a 345
  136.     set b x$a.b
  137.     set b
  138. } x345.b
  139. test parse-5.3 {variable substitution} {
  140.     set _123z xx
  141.     set b $_123z^
  142.     set b
  143. } xx^
  144. test parse-5.4 {variable substitution} {
  145.     set a 78
  146.     set b a${a}b
  147.     set b
  148. } a78b
  149. test parse-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
  150. test parse-5.6 {variable substitution} {
  151.     catch {$_non_existent_} msg
  152.     set msg
  153. } {can't read "_non_existent_": no such variable}
  154. test parse-5.7 {array variable substitution} {
  155.     catch {unset a}
  156.     set a(xyz) 123
  157.     set b $a(xyz)foo
  158.     set b
  159. } 123foo
  160. test parse-5.8 {array variable substitution} {
  161.     catch {unset a}
  162.     set "a(x y z)" 123
  163.     set b $a(x y z)foo
  164.     set b
  165. } 123foo
  166. test parse-5.9 {array variable substitution} {
  167.     catch {unset a}; catch {unset qqq}
  168.     set "a(x y z)" qqq
  169.     set $a([format x]\ y [format z]) foo
  170.     set qqq
  171. } foo
  172. test parse-5.10 {array variable substitution} {
  173.     catch {unset a}
  174.     list [catch {set b $a(22)} msg] $msg
  175. } {1 {can't read "a(22)": no such variable}}
  176. test parse-5.11 {array variable substitution} {
  177.     set b a$!
  178.     set b
  179. } {a$!}
  180. test parse-5.12 {array variable substitution} {
  181.     set b a$()
  182.     set b
  183. } {a$()}
  184. catch {unset a}
  185. test parse-5.13 {array variable substitution} {
  186.     catch {unset a}
  187.     set long {This is a very long variable, long enough to cause storage \
  188.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  189.     freed up correctly, then a core leak will occur when this test is \
  190.     run.  This text is probably beginning to sound like drivel, but I've \
  191.     run out of things to say and I need more characters still.}
  192.     set a($long) 777
  193.     set b $a($long)
  194.     list $b [array names a]
  195. } {777 {{This is a very long variable, long enough to cause storage \
  196.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  197.     freed up correctly, then a core leak will occur when this test is \
  198.     run.  This text is probably beginning to sound like drivel, but I've \
  199.     run out of things to say and I need more characters still.}}}
  200. test parse-5.14 {array variable substitution} {
  201.     catch {unset a}; catch {unset b}; catch {unset a1}
  202.     set a1(22) foo
  203.     set a(foo) bar
  204.     set b $a($a1(22))
  205.     set b
  206. } bar
  207. catch {unset a}; catch {unset a1}
  208.  
  209. # Backslash substitution.
  210.  
  211. set errNum 1
  212. proc bsCheck {char num} {
  213.     global errNum
  214.     test parse-6.$errNum {backslash substitution} {
  215.     scan $char %c value
  216.     set value
  217.     } $num
  218.     set errNum [expr $errNum+1]
  219. }
  220.  
  221. bsCheck \b    8
  222. bsCheck \e    27
  223. bsCheck \f    12
  224. bsCheck \n    10
  225. bsCheck \r    13
  226. bsCheck \t    9
  227. bsCheck \v    11
  228. bsCheck \{    123
  229. bsCheck \}    125
  230. bsCheck \[    91
  231. bsCheck \]    93
  232. bsCheck \$    36
  233. bsCheck \     32
  234. bsCheck \;    59
  235. bsCheck \\    92
  236. bsCheck \Ca    1
  237. bsCheck \Ma    225
  238. bsCheck \CMa    129
  239. bsCheck \14    12
  240. bsCheck \00a    97
  241. bsCheck b\0    98
  242. bsCheck \x    92
  243. bsCheck \
  244. a        97
  245.  
  246. test parse-7.1 {backslash substitution} {
  247.     set a "\a\c\n\]\}"
  248.     string length $a
  249. } 7
  250. test parse-7.2 {backslash substitution} {
  251.     set a {\a\c\n\]\}}
  252.     string length $a
  253. } 10
  254. test parse-7.3 {backslash substitution} {
  255.     set a "abc\
  256. def"
  257.     set a
  258. } abcdef
  259. test parse-7.4 {backslash substitution} {
  260.     set a {abc\
  261. def}
  262.     set a
  263. } "abcdef"
  264. test parse-7.5 {backslash substitution} {
  265.     set msg {}
  266.     set a xxx
  267.     set error [catch {if {24 < \
  268.     35} {set a 22} {set \
  269.         a 33}} msg]
  270.     list $error $msg $a
  271. } {0 22 22}
  272.  
  273. # Semi-colon.
  274.  
  275. test parse-8.1 {semi-colons} {
  276.     set b 0
  277.     getArgs a;set b 2
  278.     set argv
  279. } a
  280. test parse-8.2 {semi-colons} {
  281.     set b 0
  282.     getArgs a;set b 2
  283.     set b
  284. } 2
  285. test parse-8.3 {semi-colons} {
  286.     getArgs a b ; set b 1
  287.     set argv
  288. } {a b}
  289. test parse-8.4 {semi-colons} {
  290.     getArgs a b ; set b 1
  291.     set b
  292. } 1
  293.  
  294. # The following checks are to ensure that the interpreter's result
  295. # gets re-initialized by Tcl_Eval in all the right places.
  296.  
  297. test parse-9.1 {result initialization} {concat abc} abc
  298. test parse-9.2 {result initialization} {concat abc; proc foo {} {}} {}
  299. test parse-9.3 {result initialization} {concat abc; proc foo {} $a} {}
  300. test parse-9.4 {result initialization} {proc foo {} [concat abc]} {}
  301. test parse-9.5 {result initialization} {concat abc; } abc
  302. test parse-9.6 {result initialization} {
  303.     eval {
  304.     concat abc
  305. }} abc
  306. test parse-9.7 {result initialization} {} {}
  307. test parse-9.8 {result initialization} {concat abc; ; ;} abc
  308.  
  309. # Syntax errors.
  310.  
  311. test parse-10.1 {syntax errors} {catch "set a \{bcd" msg} 1
  312. test parse-10.2 {syntax errors} {
  313.     catch "set a \{bcd" msg
  314.     set msg
  315. } {missing close-brace}
  316. test parse-10.3 {syntax errors} {catch {set a "bcd} msg} 1
  317. test parse-10.4 {syntax errors} {
  318.     catch {set a "bcd} msg
  319.     set msg
  320. } {missing "}
  321. test parse-10.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
  322. test parse-10.6 {syntax errors} {
  323.     catch {set a "bcd"xy} msg
  324.     set msg
  325. } {extra characters after close-quote}
  326. test parse-10.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
  327. test parse-10.8 {syntax errors} {
  328.     catch "set a {bcd}xy" msg
  329.     set msg
  330. } {extra characters after close-brace}
  331. test parse-10.9 {syntax errors} {catch {set a [format abc} msg} 1
  332. test parse-10.10 {syntax errors} {
  333.     catch {set a [format abc} msg
  334.     set msg
  335. } {missing close-bracket}
  336. test parse-10.11 {syntax errors} {catch gorp-a-lot msg} 1
  337. test parse-10.12 {syntax errors} {
  338.     catch gorp-a-lot msg
  339.     set msg
  340. } {invalid command name: "gorp-a-lot"}
  341. test parse-10.13 {syntax errors} {
  342.     set a [concat {a}\
  343.  {b}]
  344.     set a
  345. } {a b}
  346. test parse-10.14 {syntax errors} {catch "concat \{a\}\\\n{b}" msg} 1
  347. test parse-10.15 {syntax errors} {
  348.     catch "concat \{a\}\\\n{b}" msg
  349.     set msg
  350. } {extra characters after close-brace}
  351.  
  352. # Long values (stressing storage management)
  353.  
  354. set a {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH}
  355.  
  356. test parse-11.1 {long values} {
  357.     string length $a
  358. } 214
  359. test parse-11.2 {long values} {
  360.     llength $a
  361. } 43
  362. test parse-1a1.3 {long values} {
  363.     set b "1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH"
  364.     set b
  365. } $a
  366. test parse-11.4 {long values} {
  367.     set b "$a"
  368.     set b
  369. } $a
  370. test parse-11.5 {long values} {
  371.     set b [set a]
  372.     set b
  373. } $a
  374. test parse-11.6 {long values} {
  375.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  376.     string length $b
  377. } 214
  378. test parse-11.7 {long values} {
  379.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  380.     llength $b
  381. } 43
  382. test parse-11.8 {long values} {
  383.     set b
  384. } $a
  385. test parse-11.9 {long values} {
  386.     set a [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ]
  387.     llength $a
  388. } 62
  389. set i 0
  390. foreach j [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ] {
  391.     set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
  392.     set test $test$test$test$test
  393.     set i [expr $i+1]
  394.     test parse-11.10 {long values} {
  395.     set j
  396.     } $test
  397. }
  398. test parse-11.10 {test buffer overflow in backslashes in braces} {
  399.     expr {"a" == {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101}}
  400. } 0
  401.